package ig.metrica.ui;

import ig.util.LoadProperties;
import ig.util.MyTableModel;
import java.util.Enumeration;
import java.util.Properties;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author usuario
 */
public class EditarPropiedades extends javax.swing.JInternalFrame {

    private Properties properties = null;
    private Object[][] data = new Object[50][8];
    
    
    /**
     * Creates new form EditarPropiedades
     */
    public EditarPropiedades() {
        initComponents();
        properties = LoadProperties.loadPropertiess("server/urls.properties");
        init();
        //this.table.getColumnModel().getColumn(2).setMinWidth(300);
        
        this.table.getColumnModel().getColumn(0).setMaxWidth(40);
        this.table.getColumnModel().getColumn(0).setMinWidth(40);
        
        this.table.getColumnModel().getColumn(3).setMaxWidth(50);
        this.table.getColumnModel().getColumn(3).setMinWidth(50);
        
        this.table.getColumnModel().getColumn(4).setMaxWidth(50);
        this.table.getColumnModel().getColumn(4).setMinWidth(50);
        
        this.table.getColumnModel().getColumn(5).setMaxWidth(30);
        this.table.getColumnModel().getColumn(5).setMinWidth(30);
        
        this.table.getColumnModel().getColumn(6).setMaxWidth(30);
        this.table.getColumnModel().getColumn(6).setMinWidth(30);
        
        this.table.getColumnModel().getColumn(7).setMaxWidth(60);
        this.table.getColumnModel().getColumn(7).setMinWidth(60);
    }
    
    private void init(){
        Enumeration enumeration = properties.keys();
        int i = 0;
        while (enumeration.hasMoreElements()) {
            Object key = enumeration.nextElement();
            String value = properties.getProperty(key.toString());
            data[i][0]=Boolean.valueOf(value.split("]")[0].replace("[", "").split(",")[0].toUpperCase());
            data[i][1]=key;
            data[i][2]=value.split("]")[1];
            data[i][3]=value.split("]")[0].replace("[", "").split(",")[1];
            data[i][4]=value.split("]")[0].replace("[", "").split(",")[2];
            data[i][5]=value.split("]")[0].replace("[", "").split(",")[3];
            data[i][6]=value.split("]")[0].replace("[", "").split(",")[4];
            try {
                data[i][7]=value.split("]")[0].replace("[", "").split(",")[5];
            } catch (Exception e) {
                data[i][7]=0;
            }
            i++;
        }
        for (int j = properties.values().size(); j < 50 ; j++) {
            data[j][0]=false;
            data[j][1]="";
            data[j][2]="";
            data[j][3]=100;
            data[j][4]=100;
            data[j][5]=10;
            data[j][6]=10;
            data[j][7]=0;
        }
        table.setModel(new MyTableModel(data, new String[]{"Activo","Titulo","URL","Ancho","Largo","X", "Y", "Re(Seg.)"}, new boolean[]{true,true,true,true,true,true, true, true}));
    }

    public Properties getProperties() {
        return properties;
    }

    public Object[][] getData() {
        return data;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        bAceptar = new javax.swing.JButton();
        bCancelar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle(org.openide.util.NbBundle.getMessage(EditarPropiedades.class, "EditarPropiedades.title")); // NOI18N
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/ig/util/images/metrica.gif"))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(org.openide.util.NbBundle.getMessage(EditarPropiedades.class, "EditarPropiedades.jLabel1.text")); // NOI18N

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(table);

        bAceptar.setText(org.openide.util.NbBundle.getMessage(EditarPropiedades.class, "EditarPropiedades.bAceptar.text")); // NOI18N
        bAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAceptarActionPerformed(evt);
            }
        });

        bCancelar.setText(org.openide.util.NbBundle.getMessage(EditarPropiedades.class, "EditarPropiedades.bCancelar.text")); // NOI18N
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(bCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bAceptar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bAceptar)
                    .addComponent(bCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
        this.dispose();
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_bCancelarActionPerformed

    private void bAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAceptarActionPerformed
        properties.clear();
        Object[][] data = ((MyTableModel)table.getModel()).getData();
        for (Object[] objects : data) {
            if(objects[0]!=null && !objects[0].toString().trim().isEmpty()
                &&(objects[1]!=null && !objects[1].toString().trim().isEmpty())
                &&(objects[2]!=null && !objects[2].toString().trim().isEmpty())
                &&(objects[3]!=null && !objects[3].toString().trim().isEmpty())
                &&(objects[4]!=null && !objects[4].toString().trim().isEmpty())
                &&(objects[5]!=null && !objects[5].toString().trim().isEmpty())
                &&(objects[6]!=null && !objects[6].toString().trim().isEmpty())
                &&(objects[7]!=null && !objects[7].toString().trim().isEmpty())
                    ){
                StringBuilder stringBuilder = new StringBuilder("");
                //stringBuilder.append(objects[0].toString().trim().replace(" ", "_"));
                //stringBuilder.append("=");
                stringBuilder.append("["+objects[0]);
                stringBuilder.append(","+objects[3]);
                stringBuilder.append(","+objects[4]);
                stringBuilder.append(","+objects[5]);
                stringBuilder.append(","+objects[6]);
                stringBuilder.append(","+objects[7]+"]");
                stringBuilder.append(objects[2]);
                properties.put(objects[1].toString().trim().replace(" ", "_"), stringBuilder.toString());
            }
        }
        LoadProperties.storePropertiess(properties, "server/urls.properties","Actualida...");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        PrincipalMetrica.getInstance().cargarMenuPaginasDeInteres();
        this.dispose();
    }//GEN-LAST:event_bAceptarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAceptar;
    private javax.swing.JButton bCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables

}
